package www.openteam.com.ui.business.other;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.cundong.recyclerview.EndlessRecyclerOnScrollListener;
import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;

import java.util.ArrayList;

import www.openteam.com.R;
import www.openteam.com.adapter.other.PbproductInfoAdapter;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.PbProductInfo;
import www.openteam.com.contract.business.other.SearchPbContract;
import www.openteam.com.presenter.business.other.SearchPbProductInfoPresenter;
import www.openteam.com.tool.RecyclerViewStateUtils;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.widget.LoadingFooter;

/**
 * Created by kylin on 2016/11/25.
 */

public class SearchFragment extends mBaseFragment<SearchPbProductInfoPresenter>
        implements SearchPbContract.SearchPbProductInfoView {


    private final String TAG = SearchFragment.class.getSimpleName();
    private RecyclerView mRecyclerView;
    private LinearLayoutManager linearLayoutManager;
    private PbproductInfoAdapter pbProductInfoAdapter;
    int pageSize = 0;
    int i = 0;
    private Throwable throwable;
    private ArrayList<PbProductInfo.DataEntity> pbProductInfos;
    private ArrayList<PbProductInfo.DataEntity> handlePbProductInfos;
    String name = "";
    private EditText et_search;
    private HeaderAndFooterRecyclerViewAdapter mFooterRecyclerViewAapter;
    private ButtonSelect buttonSelect;
    private ImageView iv_search;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int position = getArguments().getInt("position");
        int mark = getArguments().getInt("mark");
        Log.d(TAG, "onCreate: " + mark + ":" + position);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        iv_search = (ImageView) view.findViewById(R.id.iv_search);
        et_search = (EditText) view.findViewById(R.id.et_search);
        initRView(view);
        mRecyclerView.addOnScrollListener(mOnScrollListener);

        mPresenter = loadPresenter();
        initCommonData();
        initView();
        initListener();
        initData();
        return view;
    }


    private void initCommonData() {
        if (mPresenter != null)
            mPresenter.attachView(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createProgressDialog(getResources().getString(R.string.querying));
                name = et_search.getText().toString();
                if (pbProductInfoAdapter == null) {
                    pbProductInfos = new ArrayList<PbProductInfo.DataEntity>();
                    handlePbProductInfos = new ArrayList<PbProductInfo.DataEntity>();
                    mPresenter.queryPbproductInfo(name, pageSize);
                } else {
                    pbProductInfoAdapter.deleteItems(pbProductInfos);
                    pbProductInfos.clear();
                    throwable = null;
                    i = 0;
                    handlePbProductInfos.clear();
                }
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected SearchPbProductInfoPresenter loadPresenter() {
        return new SearchPbProductInfoPresenter();
    }

    public void initRView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_search);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);

    }

    public EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            if (state == LoadingFooter.State.Loading) {
                i++;
            }
            i++;
            if (throwable == null && (pbProductInfos.size() % 10 == 0)) {

                pbProductInfos = new ArrayList<PbProductInfo.DataEntity>();
                handlePbProductInfos = new ArrayList<PbProductInfo.DataEntity>();
                mPresenter.queryPbproductInfo(et_search.getText().toString(), i);

                RecyclerViewStateUtils.
                        setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            } else if (throwable == null && (pbProductInfos.size() % 10 != 0)) {
                //the end
                mPresenter.queryPbproductInfo(et_search.getText().toString(), i);
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            } else if (throwable != null) {
                //the end
                i = 0;
                if (throwable.toString().equals("com.google.gson.JsonSyntaxException:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 42 path $.data")) {
                    RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                    throwable = null;
                }
            }

        }
    };

    private void setPbProductInfosAdapter() {
        if (pbProductInfoAdapter == null) {
            pbProductInfoAdapter = new PbproductInfoAdapter(getActivity());
            pbProductInfoAdapter.addItems(pbProductInfos);
            pbProductInfoAdapter.addContext(getActivity());
            pbProductInfoAdapter.pbButtonSetOnclick(new PbproductInfoAdapter.PbButtonTouchEvent() {
                @Override
                public void doubleClickSelect(PbProductInfo.DataEntity pbProductInfo) {
                    buttonSelect.buttonOnclickSelect(pbProductInfo);
                }
            });
            mFooterRecyclerViewAapter = new HeaderAndFooterRecyclerViewAdapter(pbProductInfoAdapter);
            mRecyclerView.setAdapter(mFooterRecyclerViewAapter);
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        } else {
            Log.d(TAG, "setNewAdapter: 2");

            mFooterRecyclerViewAapter.notifyDataSetChanged();
            pbProductInfoAdapter.addItems(pbProductInfos);
            pbProductInfoAdapter.addContext(getActivity());
            pbProductInfoAdapter.pbButtonSetOnclick(new PbproductInfoAdapter.PbButtonTouchEvent() {
                @Override
                public void doubleClickSelect(PbProductInfo.DataEntity pbProductInfo) {
                    buttonSelect.buttonOnclickSelect(pbProductInfo);

                }

            });
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        }
    }

    @Override
    public void onRefresh() {

    }

    public interface ButtonSelect {

        void buttonOnclickSelect(PbProductInfo.DataEntity pbProductInfo);
    }


    public void buttonSetOnclick(ButtonSelect buttonSelect) {
        this.buttonSelect = buttonSelect;
    }


    @Override
    public void loadPPIData(PbProductInfo pbProductInfo) {
        mProgressDialog.dismiss();
        for (PbProductInfo.DataEntity dev : pbProductInfo.getData()) {
            pbProductInfos.add(dev);
            handlePbProductInfos.add(dev);
        }
        setPbProductInfosAdapter();
    }

    @Override
    public void hidePPIData(Throwable throwable) {
        this.throwable = throwable;
        mProgressDialog.dismiss();
        RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView,
                REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
        ToastUtil.showToast(getActivity(), throwable.getMessage());
    }

    private View.OnClickListener mFooterClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            mPresenter.queryPbproductInfo(name, i);
        }
    };


}
